﻿function packlist() {
    var p = new page();
    p.c = "Way.Bus.WMS.ST.Controllers.YS.ST_PackListController";
    p.applyTo = "tabpage1";
    p.title = "装车";
    p.mainColumns = ["ROWINDEX",'CODE', 'TOWHNO', 'STATE'];
    p.subColumns = ['THEDRIVER', 'CARNO', 'CREATEDBY', 'CREATIONDATE'];
    var where = [];
    var w1 = {  ColumnName: "CODE", Value: "", Relation: "or", Symbol: "like" };
    where.push(w1);
    p.searchWhere = where;
    p.defultWhere = [{ ColumnName: "IsSubmited", Value: "1" }, { ColumnName: "State", Value: "3", Symbol: "<" }, { ColumnName: "TheDriver", Value: webjs.userid }];

    p.fromedit = 1;
    p.cztit = "装车作业";
    p.editindex = 0;
    p.childWhere = [{ ColumnName: "STATE", Value: "3", Symbol: "<" }];
    function stys(p, f, row) {
        p.datasource.submit("ReviewedData", "ActionItem=" + webjs.jsonval(row), function (e) {
            if (e && e.error) {
                $.messager.alert('异常', e.errormsg, 'error');
            }
            else {
                f.datasource.page = "-1";
                f.loadforeign(0, [], function (e) {
                    var grid = f.griditems[0];
                    grid.load(e);
                    var obj = {};
                    obj.item = f.datasource.selectItem;
                    obj.item.STATE = 3;
                    obj.table = e;
                    var vvv = JSON.stringify(obj);
                    //navigator.geolocation.clearWatch(watchID);关闭
                    // 每隔10秒钟更新一次
                    var options = { frequency: 10000 };
                    var watchID = navigator.geolocation.watchPosition(function (e) {
                        //e.coords.latitude纬度
                        //e.coords.longitude经度
                    }, function (err) { }, options);
                    obj.item.watchID = watchID;
                    store.set("ysd", vvv);
                    $.messager.alert('消息', "开始运输中。");
                    f.close();
                });
            }
        });
    }
    p.onFromShowing = function (f, row) {
        if (row.STATE == 3) {
            f.cztitle = "卸货作业";
        }
        f.onSetValueed = function (item) {
            zcxhSet(this, item);
        }
    }

    p.onChildFrom = function (f, model, items) {
        var row = f.datasource.selectItem;
        for (var i in items) {
            var tt = items[i];
            if (tt.field == "RECEIPTCODE")
                continue;
            tt.disabled = true;
            if (tt.field == "RECEIPTDATE" || tt.field == "RECEIPTSTATE")
                tt.visible = false;
        }
    }
    function zcxhSet(f,item)
    {
        var name="";
        if (item.STATE == 3)
            name = "取消装车";
        if (item.STATE <= 2)
            name = "装车";
        var but=$("#" + f.ffnmae + "_save > span > span.l-btn-text > span");
        but.html(name);
    }
   
    p.onChange = function (f, name, newValue, oldValue) {
        if (name == "RECEIPTCODE") {
            if (newValue.length <= 5) return;
            f.clear();
            var data = f.childdata[0];
            var row;
            var item = null;
            if (data && data.rows) {
                for (var i in data.rows) {
                    row = data.rows[i];
                    if (row.RECEIPTCODE == newValue) {
                        item = row;
                        break;
                    }
                }
            }
            if (item == null && p.isonline) {
                //if ((data && data.total > 10) || (!data)) {
                    var wobj = [];
                    var w1 = { ColumnName: "RECEIPTCODE", Value: newValue };
                    wobj.push(w1);
                    f.loadforeign(0, wobj, function (e) {
                        if (e.total > 0) {
                            f.editItem = null;
                            item = e.rows[0];
                            f.notChange = true;
                            try {
                                f.onSetValue(item);
                            }
                            finally {
                                f.notChange = false;
                            }
                        }
                        else
                            $.messager.alert('异常', "扫描的签收单条码" + newValue + "不在本次任务范围!", 'error');
                    });
                //}
                //else {
                //    $.messager.alert('异常', "扫描的签收单条码" + newValue + "不在本次任务范围!", 'error');
                //}
            }
            else {
                f.editItem = null;
                f.notChange = true;
                try {
                    f.onSetValue(item);
                }
                finally {
                    f.notChange = false;
                }
            }
        }
        return false;
    }
    p.onSave = function (f, obj) {
        var item = f.datasource.selectItem;
        if (item.STATE < 3)
            zcxh("Loading", f, obj);
        if (item.STATE == 3)
            zcxh("UnLoading", f, obj);
        return false;
    }
    function zcxh(m, f, obj) {
        var packid = f.datasource.selectItem.ID;
        p.datasource.submit(m, "barcode=" + obj.RECEIPTCODE + "&packid=" + packid, function (e) {
            f.loaded();
            if (e && e.error) {
                $.messager.alert('异常', e.errormsg, 'error');
            }
            else {
                if (m == "Loading") {
                    f.isupdate = true;
                    var de = e.results;
                    if (de >= 0) {
                        f.childsearch();
                        $.messager.alert('消息', obj.RECEIPTCODE + '装车完成，剩余待装车' + de + "单。");
                        f.clear();
                    }
                    if (de < 0)
                    {
                        f.childsearch();
                        $.messager.alert('消息', obj.RECEIPTCODE + '取消装车完成。');
                        f.clear();
                    }
                }
                else {
                    $.messager.alert('消息', obj.RECEIPTCODE + '卸货完成。');
                    f.clear();
                }
            }
        });
    }
    p.init();
}